<template>
	<view class="charts-box">
		<!-- #ifdef H5-->
		<qiun-data-charts type="line" :chartData="data" :eopts="options" :echartsApp="false"/>
		<!-- #endif -->
		<!-- #ifndef H5 -->
		<qiun-data-charts type="line" :chartData="data" :eopts="options" :echartsApp="true"/>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {},
				options: {},
				chartData:{
				  series: [
					{
						name: '进水',
						type: 'line',
						showAllSymbol: true,
						symbolSize: 3,
						smooth: true,
						label: {
							show: false
						},
						lineStyle: {
							normal: {
								width: 4,
								color: "#2560F7", // 线条颜色
							},
							borderColor: 'rgba(0,0,0,.4)',
						},
						itemStyle: {
							color: "#2560F7",
							borderColor: "#646ace",
							borderWidth: 2
						},
						tooltip: {
							show: true
						},
						data: []
					  },
					{
						name: '出水',
						type: 'line',
						showAllSymbol: true,
						symbolSize: 3,
						smooth: true,
						label: {
							show: false
						},
						lineStyle: {
							width: 4,
							color: "", // 线条颜色
							borderColor: 'rgba(0,0,0,.4)',
						},
						itemStyle: {
							color: "",
						},
						tooltip: {
							show: true
						},
						data: []
					},
				  ]
				},
				eopts: {
					tooltip: {
						trigger: 'axis',
						backgroundColor:'rgba(0,0,0,.1)',
						axisPointer: {
							lineStyle: {
								color: {
									type: 'linear',
									x: 0,
									y: 0,
									x2: 0,
									y2: 1,
									colorStops: [{
										offset: 0,
										color: 'rgba(126,199,255,0)' // 0% 处的颜色
									}, {
										offset: 0.5,
										color: 'rgba(126,199,255,1)' // 100% 处的颜色
									}, {
										offset: 1,
										color: 'rgba(126,199,255,0)' // 100% 处的颜色
									}],
									global: false // 缺省为 false
								}
							},
						},
						formatter: (p) => {
							let dom = `<div style="width: 100px;
							height: 70px;;color: #000;position: relative;">
								<svg style="position: absolute;top: 50%;
							left: 50%;
							transform: translateX(-50%) translateY(-50%);" class="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="71" viewBox="0 0 84 55">
							<defs>
								<style>
								.cls-1 {
									fill: #07172c;
									fill-opacity: 0.8;
									stroke: #a7d8ff;
									stroke-linejoin: round;
									stroke-opacity: 0.2;
									stroke-width: 1px;
									fill-rule: evenodd;
								}

								</style>
							</defs>
							<path id="矩形_419" data-name="矩形 419" d="M266,595h74v50H266V624.046L261,620l5-3.984V595Z"
								transform="translate(-258.5 -592.5)" />
							</svg>
								<div style="
								padding: 4px 8px 4px 4px;
								display: flex;
								justify-content: center;
								align-items: center;
								flex-direction: column;
								position: relative;
								z-index: 1;">
									<div style="width:100%;text-align:left;">${p[0]?p[0].name:''}</div>
									<div style="margin-bottom: 4px;width:100%;display:${p[0]?'flex':'none'};justify-content:space-between;align-items:center;">
										<span style="font-size:14px;color: #7ec7ff">${p[0]?p[0].seriesName:''}</span>
										<span style="font-size:14px;color: #000">${p[0]?p[0].data:''}</span>
									</div>
									<div style="width:100%;height:100%;display:${p[1]?'flex':'none'};justify-content:space-between;align-items:center;">
										<span style="font-size:14px;color: #7ec7ff;">${p[1]?p[1].seriesName:''}</span>
										<span style="font-size:14px;color: #000;">${p[1]?p[1].data:''}</span>
									</div>
								</div>
							</div>`
									return dom
								}
							},
					legend: {
						align: "left",
						right: '1%',
						top: '3%',
						type:'plain',
						textStyle:{
							color:'#000',
							fontSize:16
						},
						// icon:'rect',
						itemGap:15,
						itemWidth:15,
						icon:'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',
						data: [
							{
								name: '进水'
							},
							{
								name: '出水'
							}
						]
					},
					grid: {
						top: '20%',
						left: '14%',
						right: '3%',
						bottom: '15%',
					},
					xAxis: [{
						type: 'category',
						boundaryGap: false,
						axisLine: { //坐标轴轴线相关设置。数学上的x轴
							show: true,
							lineStyle: {
								color: '#EDECF0'
							},
						},
						axisLabel: { //坐标轴刻度标签的相关设置
							showMaxLabel: true,
							textStyle: {
								color: '#C4C4C4',
								fontSize: 14
							}
						},
						splitLine: {
							show: true,
							lineStyle: {
								color: '#EDECF0'
							},
						},
						axisTick: {
							show: false,
						},
						data: []
					}],
					yAxis: [{
						name: '',
						nameTextStyle: {
							color: "#C4C4C4",
							fontSize: 14,
							padding: [20,0,0,-45]
						},
						min: 0,
						splitLine: {
							show: true,
							lineStyle: {
								color: '#EDECF0'
							},
						},
						axisLine: {
							show: true,
							lineStyle: {
								color: "#EDECF0"
							}

						},
						axisLabel: {
							show: true,
							fontSize: 14,
							textStyle: {
								color: '#C4C4C4',
							},
						},
						axisTick: {
							show: false,
						},
					}],
				},
				pHChartData: {
					series: [
					    {
					        data: [],
					        type: 'bar',
					        barWidth: 3,//柱子宽度
							label: {
								show: false,
							},
					        itemStyle: {//柱子样式 
					            color: '#2560F7',//背景色
					        },
					        backgroundStyle: {
					            color: 'rgba(180, 180, 180, 0.8)',
					        },
					    },
					]
				},
				pHEopts: {
					grid: {
						top: '20%',
						left: '14%',
						right: '3%',
						bottom: '15%',
					},
					xAxis: [{
						type: 'category',
						splitLine: {
							show: false,//网格线
							lineStyle: {
								color: '#EDECF0'
							},
						},
						axisTick: {
							show: false//刻度线
						},
						axisLabel: {
							show: true,//坐标值
							color: '#C4C4C4'
						},
						axisLine: {
							show: true,//坐标轴
							lineStyle: {
								color: '#EDECF0'
							},
						},
						data: [],
					}],
					yAxis: [{
						type: 'value',
						max: 8.0,
						min: 5.0,
						name: 'pH',
						nameTextStyle: {
							color: "#C4C4C4",
							fontSize: 16,
							padding: [20,0,0,-45]
						},
						axisTick: {
							show: false,//刻度线
						},
						axisLabel: {
							show: true,//坐标值
							color: '#C4C4C4',
							fontSize: 14,
						},
						axisLine: {
							show: true,//坐标轴
							lineStyle: {
								color: '#EDECF0'
							},
						},
						splitLine: {
							show: false//网格线
						},
					}],
					tooltip: {
						trigger: 'item',
					},
				}
			}
		},
		props: {
			option: {
				type: Object,
				default: {}
			},
			category: {
				type: String
			},
			params: {
				type: Object
			},
		},
		created() {
			let that = this;
			if(that.option.name == 'pH') {
				that.options = that.pHEopts
				that.data = that.pHChartData
			} else {
				that.eopts.yAxis[0].name = that.option.name;
				that.chartData.series[1].lineStyle.color = that.option.lineColor;
				that.chartData.series[1].itemStyle.color = that.option.lineColor;
				that.options = that.eopts
				that.data = that.chartData
			}
			uni.$on(that.params.in,(data) => {
				that.chartData.series[0].data = [];
				if(data.length > 0 && Object.keys(data[0]).length > 0) {
					let value = []
					let formatData = []
					const lastDateTime = that.getDateTime(data[data.length - 1].DateTime);
					for (let i = 0; i < data.length; i++) {
						let time = that.getDateTime(data[i].DateTime);
						let val = ''
						if(that.category != 'day' && that.params.in == 'Y2G2M03LL') {
							val = ((data[i].TotalVal * 1) / 1000).toFixed(1);
						} else if (that.category == 'day' && that.params.in == 'Y2G2M03LL') {
							val = ((data[i].Val * 1) / 1000).toFixed(1);
						} else {
							val = (data[i].Val * 1).toFixed(1);
						}
						formatData.push({
							val,
							time
						})
					}
					for (let i = 0; i <= lastDateTime; i++) {
						let val = '';
						for (let j = 0; j < formatData.length; j++) {
							if(i === formatData[j].time) {
								val = formatData[j].val;
							}
						}
						value.push(val)
					}
					that.chartData.series[0].data = value;
				}
			})
			uni.$on(that.params.out,(data) => {
				if(that.option.name == 'pH') {
					that.options.xAxis[0].data = [];
					that.data.series[0].data = []
				} else {
					that.options.xAxis[0].data = []
					that.data.series[1].data = []
				}
				if(data.length > 0 && Object.keys(data[0]).length > 0) {
					let label = []
					let value = []
					let formatData = []
					const lastDateTime = that.getDateTime(data[data.length - 1].DateTime);
					for (let i = 0; i < data.length; i++) {
						let val = '';
						if(that.category != 'day' && that.params.out == 'P1E1M06SSL') {
							val = ((data[i].TotalVal * 1) / 1000).toFixed(1);
						} else if(that.category == 'day' && that.params.out == 'P1E1M06SSL') {
							val = ((data[i].Val * 1) / 1000).toFixed(1);
						} else {
							val = (data[i].Val * 1).toFixed(1)
						}
						let time = that.getDateTime(data[i].DateTime);
						label.push(time);
						formatData.push({
							val,
							time
						})
					}
					for (let i = 0; i <= lastDateTime; i++) {
						let val = '';
						for (let j = 0; j < formatData.length; j++) {
							if(i === formatData[j].time) {
								val = formatData[j].val;
							}
						}
						value.push(val)
					}
					if(that.option.name == 'pH') {
						that.options.xAxis[0].data = label;
						that.data.series[0].data = value;
					} else {
						that.options.xAxis[0].data = label
						that.data.series[1].data = value;
					}
				}
			})
		},
		destroyed() {
			uni.$off(this.params.in)
			uni.$off(this.params.out)
		},
		methods: {
			getDateTime(dateTime) {
				let time = 0;
				if(dateTime) {
					let start;
					let end;
					if(this.category == 'day') {
						start = dateTime.indexOf(' ')
						end = dateTime.lastIndexOf(':')
					} else {
						start = dateTime.lastIndexOf('-') + 1
					}
					if(end) {
						time = dateTime.substring(start,end)
					} else {
						time = dateTime.substring(start)
					}
				}
				return Number(time)
			}
		}
	}
</script>

<style>
	.charts-box {
		width: 95%;
		margin: 20rpx auto;
		box-sizing: border-box;
		height: 500rpx;
		border-radius: 40rpx;
	}
</style>
